import Vue from 'vue'
import Vuex from 'vuex'
import persistedState from "vuex-persistedstate"

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: '',
    expireTime: '',
    expireTimeOut: '',
    user: '',
    power: ''
  },
  plugins: [persistedState({
    storage: window.sessionStorage,
    reducer: data => {
      return {
        token: data.token,
        expireTime: data.expireTime,
        expireTimeOut: data.expireTimeOut,
        user: data.user,
        power: data.power,
      }
    }
  })],
  mutations: {
    saveToken(state, data) {
      state.token = data
      window.sessionStorage.setItem("token", data)
    },
    saveExpireTime(state, data) {
      state.expireTime = data
      window.sessionStorage.setItem("expireTime", data)
    },
    saveExpireTimeOut(state, data) {
      state.expireTimeOut = data
      window.sessionStorage.setItem("expireTimeOut", data)
    },
    saveUser(state, data) {
      state.user = data
      window.sessionStorage.setItem("user", data)
    },
    savePower(state, data) {
      state.power = data
      window.sessionStorage.setItem("power", data)
    },
    clearAll(state) {
      state.token = '';
      state.expireTime = '';
      state.user = '';
      state.power = '';
      state.expireTimeOut = '';
      window.sessionStorage.removeItem("token")
      window.sessionStorage.removeItem("expireTime")
      window.sessionStorage.removeItem("user")
      window.sessionStorage.removeItem("power")
      window.sessionStorage.removeItem("expireTimeOut")
    }
  },
  actions: {

  }
})
